package com.example.cdctest.managecontroller;

import com.example.cdctest.pojo.Admin;
import com.example.cdctest.pojo.Config;
import com.example.cdctest.service.AdminService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/manage")
public class AdminPermissionsController {
    @Resource
    private AdminService adminService;


    //取出所有管理员
    @RequestMapping("admin-list")
    public ModelAndView adminlist()
    {
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("adminList",adminService.getadmin());
        modelAndView.setViewName("/manage/admin-list");
        return modelAndView;
    }

    @RequestMapping("admin-add")
    public String adminadd()
    {
        return "/manage/admin-add";
    }


    @RequestMapping("tianjia")
    public String tianjia(Admin admin, HttpServletRequest request) {
        System.out.println(admin.toString());

        if (admin !=null)
        {

            adminService.adminadd(admin);
            return "/manage/admin-add";
        }
        else
        {
            return "/manage/admin-add";
        }


    }

    //根据配置信息的Id,修改配置信息的状态值
    @RequestMapping("updateValues")
    @ResponseBody
    public void updateValued(Integer id, String newVals, String keys,HttpServletRequest request)
    {
      Admin admin=new Admin();
      admin.setId(id);
      if(newVals.equals("超级管理员"))
      {
          admin.setSuperAdmin(true);
      }
      else
      {
          admin.setSuperAdmin(false);
      }
     adminService.updateAdmin(admin);

    }
//停用启用管理员
    @RequestMapping("tingyong")
    @ResponseBody
    public void tingyong(Integer id)
    {
       adminService.tingyongAdmin(id);
    }

}
